<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>

import AuthorizeMasterplate from "@/components/AuthorizeMasterplate"
import unauthorizedMasterplate from "@/components/unauthorizedMasterplate"


export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base],
  components: {
    AuthorizeMasterplate,
    unauthorizedMasterplate,
  },
  data() {
    return {
      appWindow: window,
      load: false,
      showType: "",
      
      apiUrl: {
     
        wxwebEmpowerDetails: "mall-site-portal/wechat-open-config/getAuthorizerInfo",
        wxwebEmpower: "mall-site-portal/wechat-open-config/getOpenPreAuthUrl",
        wxwebDetails: "mall-site-portal/wechat-mp-config/getItem",
      },
      
      authType_id: 1,
    };
  },
  methods: {
    // 初始化
    init() {
      let _auth_code = this.$route.query.auth_code;

      if(_auth_code){
        // 解析
        let _d = encodeURI(_auth_code).split("@@@");
        this.getAuthorizeNews(encodeURI(_d[1]));
      }else{
        this.getData();
      }
    },


    // 获取公众号信息
   async  getData(){
        this.load = true;

        let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxwebDetails,
                },
        window
      );

      if(_data._ok){
        // 公众号ID
       // isWechatOpen;平台 0 否 1 是
        if(_data.data.appId && _data.data.isWechatOpen){
              this.showType = '2';
              this.wxappData = _data.data;
        }else{
          this.showType = '1';
        }
        
      }
      
      this.load = false;
   },

  // 根据AuthCode获取授权信息
   async getAuthorizeNews(value){
       this.load = true;
       let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxwebEmpowerDetails,
                    data: {
                      authType: this.authType_id,
                      authCode: value,
                    }
                },
        window
      );

      if(_data._ok){
        this.getData();
      }

     this.load = false;
   },


   // 授权
   async authorize(){
      this.load = true;
      let url = window.location.href;
   
      let _data = await this.$vFN.Http.post(
                {
                    url: this.apiUrl.wxwebEmpower,
                    data: {
                      authType: this.authType_id,
                      redirectUri: url
                    }
                },
        window
      );

      if(_data._ok){
       window.open( _data.data)
      }
      
     this.load = false;
    },


  // 列表按钮
  clickGranted(value){
     switch(value){
        case '1': 
            this.authorize();
           break;
         default:
     }
  },
 
   // 小程序详情
   clickButton(value){
     //  again:重新授权 
     this.authorize();
   },

  },
  mounted() {
    this.init();
  },
  activated() {
  },
};
</script>